package primitives;

import java.util.List;

import utility.Intersection;
import utility.Matrix4x4;
import utility.Vector3;

import com.google.common.collect.Lists;



public abstract class Shape {
// TODO move transformation stuff into xml reader
	public Shape parent;
	public List<Patch> patches;
	public Matrix4x4 worldToLocal;
	public Matrix4x4 localToWorld;
	public static int COLOR = 333;
	
	public abstract void initPatches();
	public abstract void initLight();
	public abstract Intersection<Patch,Double> intersects(Vector3 eyePosition, Vector3 pixel);
	
	public List<Patch> subdividePatches(int timesToSubdivide) {
		List<Patch> leaves = Lists.newArrayList();
		for(int i = 0; i < patches.size(); i++) {
			leaves.addAll(patches.get(i).subdivide(timesToSubdivide));
		}
		return leaves;
	}
	
	public void setInverseTransformMatrix(double scaleX, double scaleY, 
			double scaleZ, double transX, double transY, double transZ, 
			double rotX, double rotY, double rotZ) {
		Matrix4x4 transform = Matrix4x4.getInverseScaleMatrix(scaleX, 
				scaleY, scaleZ);
		transform = transform.multiply(
				Matrix4x4.getInverseRotationMatrix(rotX, rotY, rotZ));
		transform = transform.multiply(
				Matrix4x4.getInverseTranslationMatrix(transX, transY, transZ));
		worldToLocal = transform;
	}
	
	public void setTransformMatrix(double scaleX, double scaleY, 
			double scaleZ, double transX, double transY, double transZ, 
			double rotX, double rotY, double rotZ) {
		Matrix4x4 transform = Matrix4x4.getScaleMatrix(scaleX, 
				scaleY, scaleZ);
		transform = transform.multiply(
				Matrix4x4.getRotationMatrix(rotX, rotY, rotZ));
		transform = transform.multiply(
				Matrix4x4.getTranslationMatrix(transX, transY, transZ));
		this.localToWorld = transform;
	}
	
	public Matrix4x4 getTransformMatrix() {
		return localToWorld;
	}
	
	public Matrix4x4 getInverseTransformMatrix() {
		return worldToLocal;
	}

}
